package com.woniu.presell.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.woniu.common.util.Result;
import com.woniu.presell.VO.EChartsHigherVO;
import com.woniu.presell.VO.HigherVO;
import com.woniu.presell.dto.HigherDTO;
import com.woniu.presell.entity.Higher;
import com.woniu.presell.entity.Licence;
import com.woniu.presell.entity.Proposer;
import com.woniu.presell.service.HigherService;
import com.woniu.presell.service.LicenceService;
import com.woniu.presell.service.ProposerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: house-after
 * @description:
 * @author: qqq
 * @create: 2022-12-07 12:00
 **/
@RestController
@RequestMapping("/Highers")
public class HigherController {
    @Autowired
    private HigherService higherService;
    @Autowired
    private ProposerService proposerService;
    @Autowired
    private LicenceService licenceService;
    @PostMapping("/queryAll")
    public Result queryAll(@RequestBody HigherDTO higherDTO){
        List<HigherVO> highers = higherService.selectAll(higherDTO);
        PageInfo<HigherVO> higherVOPageInfo=new PageInfo<HigherVO>(highers);
        if(highers.size()>0) {
            return Result.ok().data("highers",higherVOPageInfo);
        }else {
            return Result.error();
        }
    }
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Long id){
        Map<String, Object> map = higherService.selectByHigherAndFloor(id);
        if(map.isEmpty()){
            return Result.error("未查询到楼盘，请重试");
        }else {
            return Result.ok().data(map);
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        String proForm = JSON.toJSONString(map.get("proForm"));
        String houseForm = JSON.toJSONString(map.get("houseForm"));
        String filesForm = JSON.toJSONString(map.get("filesForm"));
        Proposer proposer=JSON.parseObject(proForm,Proposer.class);
        Higher higher =JSON.parseObject(houseForm,Higher.class);
        Licence licence = JSON.parseObject(filesForm,Licence.class);
        proposerService.updateByPrimaryKeySelective(proposer);
        int add = higherService.add(higher,licence.getLicenceNo());
        int i = licenceService.updateByPrimaryKeySelective(licence, add);
        if(i>0){
            return Result.ok("提交成功").data("hid",add);
        }else{
            return Result.error("提交失败请重试");
        }
    }

    @GetMapping ("group")
    public Result selectGroupByHigher(){
        List<EChartsHigherVO> higherVOList = higherService.selectGroupByBuilding();
        return Result.ok().data("buildings",higherVOList);
    }
}
